package com.jams.goodox.activity.serve.enterprise;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.resp.EnterpriseFinancialListResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;
import com.jams.library.widget.RecyclerDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

import static com.jams.goodox.activity.serve.enterprise.EnterpriseFinancialActivity.MANAGE_PRODUCT_ID;


/**
 * 企业理财营业部列表
 */
public class FinancialDepartmentListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.empty_view_my_order_list)
    LoadView emptyView;
    @Bind(R.id.refresh_business_department)
    RefreshRecyclerView recycleBusinessDepartment;
    @Bind(R.id.srf_business_department)
    SwipeRefreshLayout refreshLayout;
    Call<EnterpriseFinancialListResp> call;
    RecycleCommonAdapter<EnterpriseFinancialListResp.DataBean.ResultListBean> departmentAdapter;
    List<EnterpriseFinancialListResp.DataBean.ResultListBean> departmentList = new ArrayList<>();
    private int curPage;
    private String department;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business_department;
    }

    @Override
    protected void init() {
        final Intent intent = getIntent();
        department = StringUtils.newString(intent.getStringExtra("TITTLE"));
        StyleUtil.titleBackKey(this, department);
        refreshLayout.setOnRefreshListener(this);
        recycleBusinessDepartment.setLayoutManager(new RefreshLinearLayoutManager(this));
        recycleBusinessDepartment.setHasFixedSize(true);
        recycleBusinessDepartment.setItemAnimator(new DefaultItemAnimator());
        departmentAdapter = new RecycleCommonAdapter<EnterpriseFinancialListResp.DataBean.ResultListBean>(mContext, departmentList, R.layout.item_bank_loan) {
            @Override
            public void convert(RecycleCommonViewHolder helper, EnterpriseFinancialListResp.DataBean.ResultListBean item, int position) {
                setItemData(helper, item);
            }
        };

        recycleBusinessDepartment.setAdapter(departmentAdapter);
        recycleBusinessDepartment.addItemDecoration(new RecyclerDivider(//分割线 1px
                mContext,
                LinearLayoutManager.VERTICAL,
                1,
                ContextCompat.getColor(getContext(),
                        R.color.shallow_divider)));
        recycleBusinessDepartment.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getData();
            }
        });
        onRefresh();
    }

    /**
     * 设置每个item数据,点击事件
     *
     * @param helper
     * @param item
     */
    private void setItemData(RecycleCommonViewHolder helper, final EnterpriseFinancialListResp.DataBean.ResultListBean item) {
        helper.setText(R.id.tv_bank_name, item.bankName)
                .setText(R.id.tv_bank_address, item.departmentName)
                .setText(R.id.tv_bank_tittle, item.lowLimit+"万起购, 利率最高"+item.highRate+"%")
                .setSimpleDraweeView(R.id.sdv_icon, StringUtils.newString(item.icon));
        helper.setOnItemClickListener(new OnRecycleItemClickListener() {
            @Override
            public void itemClick() {
                Intent i = new Intent(mContext, EnterpriseFinancialDetailActivity.class);
                i.putExtra(MANAGE_PRODUCT_ID, StringUtils.newString( item.manageProductId));
                startActivity(i);
            }
        });
    }

    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                try {
                    refreshLayout.setRefreshing(true);
                    curPage = 1;
                    getData();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 加载数据
     */
    private void getData() {
        EnterpriseService service = createCallService(EnterpriseService.class);
        if (department.equals(getResources().getString(R.string.park_business_department))) { //园区营业部
            call = service.getParkFinancialList(curPage, AppConfig.PagSize);
        } else if (department.equals(getResources().getString(R.string.other_business_department))) { //其他营业部
            call = service.getOtherFinancialList(curPage, AppConfig.PagSize);
        }
        if (call == null) {
            return;
        }
        enqueue(call, new RetrofitCallbackListener<EnterpriseFinancialListResp>() {
            @Override
            public void onSuccess(Call<EnterpriseFinancialListResp> call, EnterpriseFinancialListResp result) {
                try {
                    refreshLayout.setRefreshing(false);
                    recycleBusinessDepartment.loadMoreComplete();
                    if (result.data != null) {
                        if (curPage == 1) {
                            departmentList.clear();
                        }
                        if (result.data.pageInfo == null) {
                            recycleBusinessDepartment.setCanLoadMore(false);
                        } else {
                            recycleBusinessDepartment.setCanLoadMore(result.data.pageInfo.hasMore);
                        }
                        departmentList.addAll(result.data.resultList);
                        curPage++;
                        recycleBusinessDepartment.notifyData();
                    }
                    if (!CollectionUtils.hasData(departmentList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_department, R.string.no_department);
                    } else {
                        emptyView.loadComplete();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                try {
                    refreshLayout.setRefreshing(false);
                    recycleBusinessDepartment.loadMoreComplete();
                    if (!CollectionUtils.hasData(departmentList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_department, R.string.no_department);
                    } else {
                        emptyView.loadComplete();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }
}
